package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.dto.CommunityDto;
import net.qhzw.vip.qiyu.dto.MaintRptDto;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.param.MaintQuery;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.result.PageResult;
import net.qhzw.vip.qiyu.service.MaintRptService;
import net.qhzw.vip.qiyu.util.RedisUtils;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: MintenanceReportController
 * @Author zhang
 * @Date: 2022/7/4 15:12
 * @Version 1.0
 */

@RestController
@RequestMapping("/maint/rpt")
@Api(tags = "维修报案接口")
public class MaintRptController {

    @Autowired
    private MaintRptService maintRptService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "查询附近站点")
    @GetMapping("findNearbyCommunity")
    public JsonResult findNearbyCommunity(Page page, Double longitude, Double latitude, String keywords) {
        List<CommunityDto> communityList = maintRptService.findNearbyCommunity(page, longitude, latitude, keywords);
        Long total = maintRptService.findNearbyCommunityCount(keywords);
        PageResult result = new PageResult(total, communityList);
        return JsonResult.ok(result);
    }

    @GetMapping("getInfo")
    public JsonResult getInfo() {
        Long userId = SecurityUtils.getUserId();
        List<MaintRpt> list = maintRptService.list(new LambdaQueryWrapper<MaintRpt>()
                .lt(MaintRpt::getStatus, 5)
                .eq(MaintRpt::getRepairUserId, userId).last("ORDER BY created DESC"));
        MaintRpt maintRpt = null;
        if (!CollectionUtils.isEmpty(list)) {
            maintRpt = list.get(0);
        }
        return JsonResult.ok(maintRpt);
    }


    @ApiOperation(value = "查看用户报案队列")
    @GetMapping("list")
    public JsonResult<PageResult<MaintRpt>> list() {
        Long userId = SecurityUtils.getUserId();
        List<MaintRpt> list = maintRptService.maintQueueList(userId);
        PageResult<MaintRpt> pageResult = new PageResult<>(0L, new ArrayList<>());
        if (!CollectionUtils.isEmpty(list)) {
            pageResult = new PageResult<>(Long.valueOf(list.size()), list);
        }
        return JsonResult.ok(pageResult);
    }


    @PostMapping
    @ApiOperation(value = "用户维修报案")
    public JsonResult report(@RequestBody MaintRpt param) {
        boolean isOK = maintRptService.report(param);
        if (isOK)
            return JsonResult.ok();
        return JsonResult.fail();
    }


    @ApiOperation(value = "查看用户报案信息")
    @GetMapping("getUserReport")
    public JsonResult getUserReport(Page<MaintRptDto> page, String maintRealName, String maintMobile) {
        Long userId = SecurityUtils.getUserId();
        PageResult<MaintRptDto> maintRptPage = maintRptService.findUserReport(page, userId, maintRealName, maintMobile);
        return JsonResult.ok(maintRptPage);
    }

    @ApiOperation(value = "查看用户报案信息列表")
    @GetMapping
    public JsonResult getMaintenanceList(Page<MaintRptDto> page, MaintQuery query) {
        PageResult<MaintRptDto> maintRptPage = maintRptService.findMaintenanceList(page, query);
        return JsonResult.ok(maintRptPage);
    }

    @ApiOperation(value = "查看用户报案信息列表")
    @GetMapping("/getMaintRptRepair")
    public JsonResult getMaintenanceList1() {
        List<MaintRptDto> maintRptPage = maintRptService.selectMaintenanceList();
        return JsonResult.ok(maintRptPage);
    }


    @ApiOperation(value = "手动取消报修申请")
    @PutMapping("cancelMain/{id}")
    public JsonResult cancelMaintenance(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        MaintRpt maintRpt = maintRptService.getById(id);
        if (maintRpt.getStatus().intValue() == 1 || maintRpt.getStatus().intValue() == 2 || maintRpt.getStatus().intValue() == 7) {
            maintRpt.setStatus((byte) 9);
            redisTemplate.boundHashOps("maintQueue").delete(userId);
            boolean isOK = maintRptService.updateById(maintRpt);
            if (isOK) {
                return JsonResult.ok();
            }
        }else {
            throw new ApiException("维修人员已接单,暂无法取消订单, 请联系管理员!");
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "后台取消订单")
    @PutMapping("closureOrder/{id}")
    public JsonResult closureOrder(@PathVariable Long id) {
        MaintRpt maintRpt = maintRptService.getById(id);
        maintRpt.setStatus((byte) 8);
        redisTemplate.boundHashOps("maintQueue").delete(maintRpt.getUserId());
        redisTemplate.boundHashOps("maintTasks").delete(maintRpt.getRepairUserId());
        boolean isOK = maintRptService.updateById(maintRpt);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
